package com.hcx.controller;

import com.hcx.Service.IChatHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class ChatController {

    private final ChatClient chatClient;
    private final IChatHistoryService chatHistoryService;
    // 请求方式和路径不要改动，将来要与前端联调
    @PostMapping (value ="/chat",produces = "text/html;charset=UTF-8")
    public Flux<String> chat( @RequestParam( name = "prompt",defaultValue = "讲个笑话") String prompt, @RequestParam(name = "chatId", required = false) String chatId) {
        chatHistoryService.saveChatHistory("chat",chatId);
        return chatClient
                .prompt(prompt) // 传入user提示词
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, chatId))
                .stream() // 流式调用
                .content(); //返回响应内容
    }
}